﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.Plumbing;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YArchitech.HVAC
{
	internal class LayDuctTerminalDrawUtility
	{
		public static List<Line> RectDraw(Autodesk.Revit.DB.View view, XYZ startPoint, XYZ currentPoint, RectLayParamers rectLayParamers)
		{
			List<Line> list = new List<Line>();
			try
			{
				double num = 1.6404199475065617 * (double)view.Scale / 100.0;
				double num2 = 1.6404199475065617 * (double)view.Scale / 100.0;
				List<XYZ> list2 = new List<XYZ>();
				if (rectLayParamers != null)
				{
					list2 = LayDuctTerminalDrawUtility.GetLocationsByRectLay(startPoint, currentPoint, rectLayParamers);
				}
				if (list2.Count > 0)
				{
					foreach (XYZ xyz in list2)
					{
						XYZ xyz2 = xyz + view.RightDirection * num / 2.0 + view.UpDirection * num2 / 2.0;
						XYZ xyz3 = xyz - view.RightDirection * num / 2.0 + view.UpDirection * num2 / 2.0;
						XYZ xyz4 = xyz - view.RightDirection * num / 2.0 - view.UpDirection * num2 / 2.0;
						XYZ xyz5 = xyz + view.RightDirection * num / 2.0 - view.UpDirection * num2 / 2.0;
						list.Add(YJKLineEx.YJKGetBound(xyz2, xyz3));
						list.Add(YJKLineEx.YJKGetBound(xyz3, xyz4));
						list.Add(YJKLineEx.YJKGetBound(xyz4, xyz5));
						list.Add(YJKLineEx.YJKGetBound(xyz5, xyz2));
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return list;
		}

		public static List<Line> CenterDraw(Autodesk.Revit.DB.View view, XYZ startPoint, XYZ currentPoint, LayMidParameter centerLayParamers)
		{
			List<Line> list = new List<Line>();
			try
			{
				double num = 1.6404199475065617 * (double)view.Scale / 100.0;
				double num2 = 1.6404199475065617 * (double)view.Scale / 100.0;
				List<XYZ> list2 = new List<XYZ>();
				if (centerLayParamers != null)
				{
					if (centerLayParamers.Type == 0)
					{
						list2 = LayDuctTerminalDrawUtility.GetLocationsByCenterSpace(startPoint, currentPoint, centerLayParamers);
					}
					else
					{
						list2 = LayDuctTerminalDrawUtility.GetLocationsByCenterRowCow(startPoint, currentPoint, centerLayParamers);
					}
				}
				if (list2.Count > 0)
				{
					foreach (XYZ xyz in list2)
					{
						XYZ xyz2 = xyz + view.RightDirection * num / 2.0 + view.UpDirection * num2 / 2.0;
						XYZ xyz3 = xyz - view.RightDirection * num / 2.0 + view.UpDirection * num2 / 2.0;
						XYZ xyz4 = xyz - view.RightDirection * num / 2.0 - view.UpDirection * num2 / 2.0;
						XYZ xyz5 = xyz + view.RightDirection * num / 2.0 - view.UpDirection * num2 / 2.0;
						list.Add(YJKLineEx.YJKGetBound(xyz2, xyz3));
						list.Add(YJKLineEx.YJKGetBound(xyz3, xyz4));
						list.Add(YJKLineEx.YJKGetBound(xyz4, xyz5));
						list.Add(YJKLineEx.YJKGetBound(xyz5, xyz2));
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return list;
		}

		private static List<XYZ> GetLocationsByRectLay(XYZ basePoint, XYZ currentPoint, RectLayParamers rectLayParamers)
		{
			List<XYZ> list = new List<XYZ>();
			rectLayParamers.PointBase = basePoint;
			rectLayParamers.PointDiagonal = currentPoint;
			foreach (List<XYZ> list2 in new RectLayPoint(rectLayParamers).PointSet)
			{
				foreach (XYZ item in list2)
				{
					list.Add(item);
				}
			}
			return list;
		}

		public static List<XYZ> GetLocationsByCenterSpace(XYZ basePoint, XYZ currentPoint, LayMidParameter centerLayParamers)
		{
			return new ConnectHelper().CalculateLayFamilyInstanceXYZ(basePoint, currentPoint, centerLayParamers.RowCount, centerLayParamers.ColCount, centerLayParamers.LeftDistance, centerLayParamers.RightDistance, centerLayParamers.TopDistance, centerLayParamers.BottomDistance, UnitConvert.CovertToAPI(LayDuctTerminalDrawUtility.airTerminalPara.OffSet * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
		}

		public static List<XYZ> GetLocationsByCenterRowCow(XYZ basePoint, XYZ currentPoint, LayMidParameter centerLayParamers)
		{
			return new ConnectHelper().CalculateLayFamilyInstanceXYZ(basePoint, currentPoint, centerLayParamers.RowDistance, centerLayParamers.ColDistance, UnitConvert.CovertToAPI(LayDuctTerminalDrawUtility.airTerminalPara.OffSet * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
		}

		public static AirTerminalParamNew airTerminalPara;
	}
}
